'''
https://leetcode.cn/problems/unique-substrings-in-wraparound-string/description/
'''


class Solution:

    def findSubstringInWraproundString(self, s: str) -> int:
        s = [ord(c) - ord('a') for c in s]
        dp = [0] * 26
        dp[s[0]] = 1
        max_len = 1
        for i in range(1, len(s)):
            pre = s[i-1]
            if pre + 1 == s[i] or (pre == 25 and s[i] == 0):
                max_len += 1
            else:
                max_len = 1
            dp[s[i]] = max(dp[s[i]], max_len)
        return sum(dp)
